import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '权限名称',		name: 'perm_name' },
			{ label: '权限代码',		name: 'perm_code' },
			{ label: '权限类型',		name: 'perm_type',	type: 'select', loading:true, idStr:'code' },
			{ label: '权限级别',		name: 'level',		type: 'select', idStr:'code' },
		],
		// 提交表单列表
		formData : [
			{ label: '权限名称',		name: 'perm_name', 		required:true, half:true },
			{ label: '英文名称',		name: 'perm_name_en', 	required:true, half:true },
			{ label: '权限代码',		name: 'perm_code', 		required:true, half:true },
			{ label: '排序',			name: 'sort', 			half:true,value:0 },
			{ label: '权限级别',		name: 'level',			type: 'select', required:true, half:true, loading:true, idStr:'code' },
			{ label: '权限类型',		name: 'perm_type',		type: 'select', required:true, half:true, idStr:'code' },
			{ label: '权限描述',		name: 'perm_desc', 		full:true,         type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		const uuid = $fn.query('uuid')
		if(uuid){
			this.uuid = uuid
			this.query = { uuid }
		}
		
		this.getSelect()
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 权限类型
			$fn.setRows([ forms, formData ], 'perm_type', await f.getDicCache({ code:900 } ) )
			// 权限级别
			$fn.setRows([ forms, formData ], 'level',  await f.getDicCache({ code:800 } ))
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'permission/index')
	
	// 表单
	cols = [
		{ title: 'ID', 			dataIndex: 'id', 				width:50 },
		{ title: '权限名称', 	dataIndex: 'perm_name', 		width:300 },
		{ title: '权限代码', 	dataIndex: 'perm_code', 		width:250	},
		{ title: '权限类型', 	dataIndex: 'type', 				width:100 },
		{ title: '权限级别', 	dataIndex: 'level', 			width:80 },
		{ title: '权限描述', 	dataIndex: 'perm_desc', 		width:300, 	},
		{ title: '创建时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this)
	]
	
	
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'permission/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),													// 删除
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onDel(),						// 删除
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'permission/delete', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='权限库' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'permission/add', editApi:'permission/edit' }) }
			</>
		)
	}
}

export default Index